﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web4Comment.Service
{
    //public static class NewsService:INewsService
    public static class NewsService
    {
        public static int recommendNews(string from_username, string to_username, string news_url)
        {
            int from_id = Service.UserService.getUserid(from_username);
            int to_id = Service.UserService.getUserid(to_username);
            int news_id = getNewsId(news_url);
            if (from_id != 0 && to_id != 0 && news_id != 0)
            {             
                UnlessComment.BLL.Recommender recommenderBLL = new UnlessComment.BLL.Recommender();
                if (recommenderBLL.GetModelList("news_id = " + news_id + " and fromUser_id = " + from_id + " and toUser_id = " + to_id).Count == 0)
                {
                    UnlessComment.Model.Recommender recommender = new UnlessComment.Model.Recommender();
                    recommender.fromUser_id = from_id;
                    recommender.toUser_id = to_id;
                    recommender.news_id = news_id;
                    recommender.state = 1;
                    return recommenderBLL.Add(recommender);
                }
                else
                {
                    return 0;
                }
            }
            else
                return 0;
        }

        public static List<UnlessComment.Model.News> getRecommendNews(int userId)
        {
            return null;
        }

        public static int addNews(string url, string content)
        {
            UnlessComment.BLL.News newsBLL = new UnlessComment.BLL.News();
            if (newsBLL.GetModelList("url = '" + url + "'").Count > 0)
                return 0;
            UnlessComment.Model.News news = new UnlessComment.Model.News();
            news.content = content;
            news.date = DateTime.Now;
            news.state = 1;
            news.url = url;
            news.catalog = 1;
            return newsBLL.Add(news);
        }

        public static int deleteNews(int newsId)
        {
            return 0;
        }

        public static UnlessComment.Model.News getSingleNews(string url)
        {
            UnlessComment.BLL.News newsBLL = new UnlessComment.BLL.News();
            List<UnlessComment.Model.News> newsList = newsBLL.GetModelList("url = '" + url + "'");
            if (newsList.Count <= 0)
                return null;
            return newsList[0];
        }

        public static int getNewsId(string url)
        {
            UnlessComment.BLL.News newsBLL = new UnlessComment.BLL.News();
            List<UnlessComment.Model.News> newsList = newsBLL.GetModelList("url = '" + url + "'");
            if (newsList.Count <= 0)
                return 0;
            return newsList[0].id;
        }

    }
}